package com.example.mybank.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybank.entity.Product;
import com.example.mybank.entity.Schedule;
import com.example.mybank.entity.Users;
import com.example.mybank.mapper.ProductMapper;
import com.example.mybank.mapper.ScheduleMapper;
import com.example.mybank.springSecurity.UserHandler;
import com.example.mybank.tool.ResultCode;
import com.example.mybank.tool.MyException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/schedule")
public class ScheduleController {
    @Autowired
    ScheduleMapper scheduleMapper;

    @Autowired
    ProductMapper productMapper;

    // 管理员：查看所有用户的预约
    @GetMapping("/all")
    public List<Schedule> getAll(){
        return scheduleMapper.selectList(null);
    }


    // 用户：查看预约
    @GetMapping("/user/{usersID}")
    public List<Schedule> getByUser(@PathVariable("usersID") int userID){
        QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("users",userID);
        return scheduleMapper.selectList(queryWrapper);
    }

    /**
     * 新增预约
     * @param projectID
     * @return
     * @throws MyException
     */
    @GetMapping("/add/{projectID}")
    public Schedule add(@PathVariable("projectID") Long projectID) throws MyException{
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", projectID);
        Product selPro = productMapper.selectOne(queryWrapper);

        // 判断是否已预约
        QueryWrapper<Schedule> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("product", projectID);
        if(scheduleMapper.selectOne(queryWrapper2) != null)
            throw new MyException(ResultCode.Schedule_Exist);
        
        // 获取用户
        UserHandler principal = (UserHandler)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Users user = principal.getUsers();

        // 生成预约
        Schedule add = new Schedule();
        add.setUsers(user.getId());
        add.setProduct(selPro.getId());
        add.setDatetime_create(Calendar.getInstance().getTime());
        add.setDatetime_from(selPro.getTime_from());
        add.setDatetime_to(selPro.getTime_end());
        scheduleMapper.insert(add);

        return add;
    }

    @GetMapping("/cancel/{productID}")
    public Boolean cancel(@PathVariable("productID") Long productID) throws MyException{

        UserHandler principal = (UserHandler)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Users users = principal.getUsers();

        QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("users",users.getId());
        queryWrapper.eq("product",productID);
        Schedule schedule = scheduleMapper.selectOne(queryWrapper);

        if(schedule==null){
            throw new MyException(ResultCode.Schedule_NOT_EXIST);
        }

        scheduleMapper.deleteByUserProduct(users.getId(), productID);
        return Boolean.TRUE;
    }
}
